<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public function index(){		
		$us = $_GET['us'];
		$un = $_GET['un'];
		if($this->session->userdata('us') != '' && $this->session->userdata('un') != ''){
			if (($us == '') && ($un == '')){
				$us = $this->session->userdata('us');
				$un = $this->session->userdata('un');
			}
		}
		if($us == '' || $un == ''){
			$this->load->helper(array('url'));
			redirect('http://tiennhanh.com/logreport.php', 'location', 301);
		}else{
			$this->session->set_userdata(array('us' => $us, 'un' => $un));
			$query = $this->db->query("SELECT * FROM sms_user WHERE user_check = '" . $us . "' and user_name= '" . $un . "'");
			if ($query->num_rows() > 0){
				$row = $query->row_array();
				if ($row['status'] == 0){					
					$result['error'] = true;
					$result['msg'] = '<b><span class="red">Tài khoản chưa kích hoạt!</span></b><br />';
					$result['msg1'] = "Hãy soạn tin nhắn:";
					$result['smscode'] = '<b><span class="red">GN ' . $un . '</span></b> gửi <b><span class="red">8013</span></b>';
					$result['msg2'] = "để kích hoạt tài khoản của bạn.";
					$this->layout->setLayout('cupcake_login');
					$this->layout->setTitle('Bạn chưa kích hoạt tài khoản để sử dụng dịch vụ');
					$this->layout->view(THEME_DIR.'/website_login', $result);
				}else{
					$querychild = $this->db->query("SELECT * FROM sms_user WHERE parent_id = '" . $row['user_id_reg'] . "'");
					if ($querychild->num_rows() > 0){
						$data['uparentid'] = 1;
					}
					$this->layout->setTitle('Doanh thu trong tháng của bạn');
					$uid = $row['user_id_reg'];
					$smscode = "khr" . $uid . "m";
					$smsandroi = "khr" . $uid . "ma";
					$smsjava = "khr" . $uid . "mj";
					$smsiphone = "khr" . $uid . "mi";
					
					$month = isset($_GET['month']) ? quotes_to_entities($_GET['month']) : date('m');
					$year = isset($_GET['year']) ? $_GET['year'] : date('Y'); 
					if ($year == "") $year = date('Y');
					if ($month == "") $month = date('m');
					$getmonth = $month;
					$month = $year . '-'. $month;
					$arrDate = explode("-", $month);
					//Tinh so ngay trong thang
					$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
					//So luong sms tung dong may
					$sqlwebsite = "SELECT B.earn_date, B.id_reg, SUM(B.java_SMS) AS java_SMS, SUM(B.java_earm) AS java_earm, SUM(B.apk_sms) as apk_sms, SUM(B.apk_earm) as apk_earm, SUM(B.ios_sms) as ios_sms, SUM(B.ios_earm) as ios_earm, (Sum(B.java_earm) + Sum(B.apk_earm) + SUM(B.ios_earm)) as Total
			FROM (SELECT  DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, T.id_reg, count(T.phone) AS java_SMS, Sum(T.sms_cost) AS java_earm, '0' as apk_sms, '0' as apk_earm, '0' as ios_sms, '0' as ios_earm
					FROM `sms_user_mo` T
			WHERE  1=1 AND T.message_request like '$smsjava%' AND T.receive_date BETWEEN '" . $month . "-01 00:00:00' AND '" . $month . "-" . $day . " 23:59:59'
			GROUP BY earn_date
			Union
			SELECT  DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, T.id_reg, '0' AS java_SMS, '0' AS java_earm, count(T.phone) as apk_sms, Sum(T.sms_cost) as apk_earm, '0' as ios_sms, '0' as ios_earm
					FROM `sms_user_mo` T
			WHERE  1=1 AND T.message_request like '$smsandroi%' AND T.receive_date BETWEEN '" . $month . "-01 00:00:00' AND '". $month . "-" . $day . " 23:59:59'
			GROUP BY earn_date
			Union
			SELECT  DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, T.id_reg, '0' AS java_SMS, '0' AS java_earm, '0' as apk_sms, '0' as apk_earm, count(T.phone) as ios_sms, Sum(T.sms_cost) as ios_earm
					FROM `sms_user_mo` T
			WHERE  1=1 AND T.message_request like '$smsiphone%' AND T.receive_date BETWEEN '" . $month . "-01 00:00:00' AND '". $month . "-" . $day . " 23:59:59'
			GROUP BY earn_date
			) B
			INNER JOIN `sms_user` U On U.user_id_reg = B.id_reg
			GROUP BY earn_date";
					//ket thuc					
			
					$monthQuerysms = $this->db->query($sqlwebsite);
					if ($monthQuerysms->num_rows() > 0){
						$arrData = array();
						foreach ($monthQuerysms->result_array() as $row){					
							$data['items2'][] = $row;
						}
					}
					
					$filmonth =  " AND T.message_request like '" . $smscode . "%' AND T.receive_date BETWEEN '" . $month . "-01 00:00:00" . "' AND '" . $month . "-31 23:59:59" . "'";
					
					$sqlwebsite = "SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, Sum(T.sms_cost) As subtotal
					FROM `sms_user_mo` T
					WHERE  1=1 $filmonth
					Group by earn_date
					Order By id";
					$monthQuery = $this->db->query($sqlwebsite);
					// get total
					if ($monthQuery->num_rows() > 0){
						$arrData = array();
						$total = 0;
						$intMonth = substr($monthQuery->row(0)->earn_date,0,2);
						$month2 = $getmonth;
						if (($getmonth < 10) && strlen($getmonth) == 1 ) $month2 = '0' . $getmonth;
						$Y = substr($monthQuery->row(0)->earn_date,6,4);
						$data['month'] = $month2 .'-'.$Y; 
						if ($intMonth > 1){					
							for ($i=1;$i<=$intMonth - 1;$i++):					
								if ($i < 10)
									$arrData['0'. $i . '-' . $month2 . '-' . $Y] = 0;
								else
									$arrData[$i . '-' . $month2 .'-'. $Y] = 0;
							endfor;
							$dd = $i . '-' . $month2 .'-'. $Y;
						}
						foreach ($monthQuery->result_array() as $row){					
							$arrData[$row['earn_date']] += intval($row['subtotal']);
							$total += intval($row['subtotal']);
						}
					}
					
					$strDate = 'Hôm nay ('.date('d-m-Y').')';// .$pos.'--'.$sqlwebsite;
					$data['earn_today'] = intval($arrData[date('d-m-Y')]);
				
					$data['earn_yesterday'] = intval($arrData[date('d-m-Y', strtotime('-1 day'))]);
					$data['earn_this_month'] = $total;
					$data['str_date'] = $strDate;
					$data['month_data'] = $arrData;
					
					$this->layout->view(THEME_DIR.'/report_index', $data);
				}
			}else{
				$this->load->helper(array('url'));
				redirect('http://tiennhanh.com/logreport.php', 'location', 301);
			}			
		}
	}	
}